﻿//------------------------------------------------------------------------------
// System Name:   Customer Service Platform
// Component:     CCS Action Point Owners & Contributers 
// Modified By:   Leo ren (leoren@wicresoft.com)
// Modified Date: 2011-2-11
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BEGDC.CC.BL.CCFlow.EV;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.BL.Email;
using BEGDC.CC.Entity;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.UserControls.EV
{
    /// <summary>
    /// Summary description for APCommentCtrl1.
    /// </summary>
    public class APCommentCtrl1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lblNo;
        protected System.Web.UI.WebControls.Label lblAPCommentNo;
        protected System.Web.UI.WebControls.Label lblEvent;
        protected System.Web.UI.WebControls.Label lblAnswered;
        protected System.Web.UI.WebControls.TextBox txtAnswered;
        protected System.Web.UI.WebControls.Label lblIncluding;
        protected System.Web.UI.WebControls.TextBox txtIncluding;
        protected System.Web.UI.WebControls.TextBox txtReply;
        protected System.Web.UI.WebControls.Button ibtnSubmitComment;
        protected System.Web.UI.WebControls.CheckBoxList cbListAnsweredBy;

        protected int APID
        {
            get
            {
                if (Request.QueryString["APID"] == null)
                {
                    if (ViewState["APID"] != null)
                    {
                        return Convert.ToInt32(ViewState["APID"]);
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return Convert.ToInt32(Request.QueryString["APID"]);
                }
            }
            set
            {
                ViewState["APID"] = value;
            }
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if (!IsPostBack)
            {
                InitCommentInfo();
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.ibtnSubmitComment.Click += new EventHandler(ibtnSubmitComment_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void InitCommentInfo()
        {
            IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
            this.lblAPCommentNo.Text = Convert.ToString(evService.GetMaxAPCommentID(this.APID) + 1);
            this.txtAnswered.Text = HttpContext.Current.User.Identity.Name;

            this.txtReply.Text = "";
            this.txtIncluding.Text = "";

            //Bind Owner & Contributer

            IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
            BEGDC.CC.BL.Workflow.CCIdentity identity = (BEGDC.CC.BL.Workflow.CCIdentity)this.Page.User.Identity;
            DataTable ownerContributerTable = queryService.GetAssignedDeptHeaders(this.APID, identity.UserInfo.Id);
            this.cbListAnsweredBy.DataSource = ownerContributerTable.DefaultView;
            this.cbListAnsweredBy.DataTextField = "NameEN";
            this.cbListAnsweredBy.DataValueField = "EmployeeNo";
            this.cbListAnsweredBy.DataBind();
        }

        private Entity.APComment BuildComment2Save(string answeredBy)
        {
            APComment comment = new APComment();
            comment.APNo = Convert.ToInt32(this.lblAPCommentNo.Text);
            comment.Events = this.txtReply.Text;
            comment.AnsweredBy = answeredBy;
            comment.Including = this.txtIncluding.Text;
            comment.Date = new Nullables.NullableDateTime(System.DateTime.Now);

            IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
            comment.ActionPoint = evService.GetAPByID(this.APID);

            return comment;
        }

        void ibtnSubmitComment_Click(object sender, EventArgs e)
        {
            try
            {
                IEV evService = AppContainer.Instance[ServiceConst.EVService] as IEV;
                //Save Answered Person
                evService.SubmitAPComment(this.BuildComment2Save(this.txtAnswered.Text));
                //Save Including persons
                foreach (ListItem item in this.cbListAnsweredBy.Items)
                {
                    if (item.Selected)
                    {
                        //this.txtIncluding.Text += "," + item.Text;
                        evService.SubmitAPComment(this.BuildComment2Save(item.Text));
                    }
                }

                //Send mail
                IEmail email = AppContainer.Instance[ServiceConst.EmailService] as IEmail;
                int appid = evService.GetAPByID(this.APID).EVMeeting.Application.Id;
                //<--joan 2007/09/17
                email.Send(appid, this.APID, EmailTemplate.APComment);
                //-->end

                Response.Write("<script>window.returnValue='reload';window.close();</script>");
                //Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.returnValue='reload';window.close();</script>");
                //InitCommentInfo();
            }
            catch
            {
            }

        }
    }
}
